home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
MacWorld 1997 September
/
Macworld (1997-09).dmg
/
Shareware World
/
Utilities
/
Text Processing
/
Alpha
/
Tcl
/
Modes
/
latexComm.tcl
< prev
next >
Wrap
Text File
|
1996-09-22
|
19KB
|
672 lines
#############################################################################
#############################################################################
#
# latexComm.tcl (called from latex.tcl)
#
#############################################################################
#
# Author: Tom Scavo <trscavo@syr.edu>
#
#############################################################################
#############################################################################
#--------------------------------------------------------------------------
# TeX applications
#--------------------------------------------------------------------------
# Application *names* aren't needed any more!
# In the following scripts, $quotedSig and $filename are the application
# signature and the name of the file to be typeset, respectively. (See
# proc 'evalTeXScript' below.)
# OzTeX:
# set texAppName(OzTeX) {*OzTeX*}
set texAppSig(OzTeX) {OTEX}
set texAppScripts(OzTeX) {
{sendOpenEvent noReply $quotedSig $filename}
}
# Textures:
# set texAppName(Textures) {*Textures*}
set texAppSig(Textures) {*TEX}
# This is the old Textures typesetting script:
# set texAppScripts(Textures) {{dosc -c $quotedSig -f $filename -r}}
set texAppScripts(Textures) {
{global TeXconnections}
{set TeXjob ""}
{
foreach entry $TeXconnections {
if { [car $entry] == $filename } {
set TeXjob [cadr $entry]
break
}
}
}
{
if { $TeXjob == "" } {
set TeXjob [AEBuild -r $quotedSig BSRs Begi]
set TeXjob [string trim [string range $TeXjob 15 end] {\{\}\"}]
lappend TeXconnections [list $filename $TeXjob]
}
}
{AEBuild -t 1200 $quotedSig BSRs Typs "----" [makeAlis $filename] fmat {"LaTeX"} Jobi $TeXjob}
}
# CMacTeX:
# set texAppName(CMacTeX) {*tex}
set texAppSig(CMacTeX) {*XeT}
set texAppScripts(CMacTeX) {
{dosc -c $quotedSig -k 'aevt' -e 'odoc' -r -f $filename}
}
# DirectTeX:
# set texAppName(DirectTeX) {MPW*}
set texAppSig(DirectTeX) {MPS*}
set texAppScripts(DirectTeX) {
{set script "Begin; ChangeTeXProject '$filename' -check -confirm || Exit 0; \
Execute \"{dt_TeXProject}\"; TeXMenu -tex -formats; \
End ∑ Dev:Null; RunSession 1 ∑ Dev:Null"}
{dosc -r -c $quotedSig -s $script}
}
# DirectTeX Pro:
# set texAppName(DirectTeXPro) {DirectTeX*}
set texAppSig(DirectTeXPro) {TeX+}
set texAppScripts(DirectTeXPro) {
{dosc -c $quotedSig -s \
"if \$dt_TeXFormat = \'\' \"set dt_TeXFormat -x \$dt_DefaultFormat\""}
{dosc -c $quotedSig -s \
"ProjectMgr -t \$dt_TeXFormat \"$filename\"; MenuCommand 1 4"}
}
#--------------------------------------------------------------------------
# DVI viewers
#--------------------------------------------------------------------------
# OzTeX:
# set viewDVIAppName(OzTeX) $texAppName(OzTeX)
set viewDVIAppSig(OzTeX) $texAppSig(OzTeX)
set viewDVIAppScripts(OzTeX) {
{sendOpenEvent noReply $quotedSig $filename}
}
# Textures:
# set viewDVIAppName(Textures) $texAppName(Textures)
set viewDVIAppSig(Textures) $texAppSig(Textures)
set viewDVIAppScripts(Textures) {
{AEBuild $quotedSig aevt odoc "----" [makeAlis $filename]}
}
# CMacTeX:
# set viewDVIAppName(CMacTeX) dvipreview
set viewDVIAppSig(CMacTeX) {PIVD}
set viewDVIAppScripts(CMacTeX) {
{dosc -c $quotedSig -k 'aevt' -e 'odoc' -r -f $filename}
}
# DirectTeX Pro:
# set viewDVIAppName(DirectTeXPro) $texAppName(DirectTeXPro)
set viewDVIAppSig(DirectTeXPro) $texAppSig(DirectTeXPro)
# set viewDVIAppScripts(DirectTeXPro) {
# {dosc -c $quotedSig -s \
# "ProjectMgr -t LaTeX \"$filename\"; SelectApp; MenuCommand 1 8"}
# }
set viewDVIAppScripts(DirectTeXPro) {
{dosc -c $quotedSig -s "ProjectMgr -t \$dt_TeXFormat \"[lindex [winNames -f] 0]\"; MenuCommand 1 8"}
}
#--------------------------------------------------------------------------
# DVI print drivers
#--------------------------------------------------------------------------
# OzTeX:
# set printDVIAppName(OzTeX) $texAppName(OzTeX)
set printDVIAppSig(OzTeX) $texAppSig(OzTeX)
set printDVIAppScripts(OzTeX) {
{dosc -c $quotedSig -k 'aevt' -e 'pdoc' -r -f $filename}
}
# Textures:
# set printDVIAppName(Textures) $texAppName(Textures)
set printDVIAppSig(Textures) $texAppSig(Textures)
set printDVIAppScripts(Textures) {
{AEBuild $quotedSig aevt pdoc "----" [makeAlis $filename]}
}
# set printDVIAppScripts(Textures) \
# {{dosc -c $quotedSig -k 'aevt' -e 'pdoc' -r -f $filename}}
# CMacTeX:
# set printDVIAppName(CMacTeX) printdvi
set printDVIAppSig(CMacTeX) {CMT8}
set printDVIAppScripts(CMacTeX) {
{dosc -c $quotedSig -k 'aevt' -e 'pdoc' -r -f $filename}
}
# DirectTeX Pro:
# set printDVIAppName(DirectTeXPro) $texAppName(DirectTeXPro)
set printDVIAppSig(DirectTeXPro) $texAppSig(DirectTeXPro)
set printDVIAppScripts(DirectTeXPro) {
{dosc -c $quotedSig -s \
"ProjectMgr -t \$dt_TeXFormat \"[lindex [winNames -f] 0]\"; MenuCommand 1 9"}
}
#--------------------------------------------------------------------------
# DVI-to-PS filters
#--------------------------------------------------------------------------
# OzTeX:
# set dvipsAppName(OzTeX) OzDVIPS
set dvipsAppSig(OzTeX) {OzDP}
set dvipsAppScripts(OzTeX) {
{sendOpenEvent noReply $quotedSig $filename}
}
# CMacTeX:
# set dvipsAppName(CMacTeX) dvips
set dvipsAppSig(CMacTeX) {CMT1}
set dvipsAppScripts(CMacTeX) {
{dosc -c $quotedSig -k 'aevt' -e 'odoc' -t 600 -f $filename}
}
# DirectTeX Pro:
# set dvipsAppName(DirectTeXPro) $texAppName(DirectTeXPro)
set dvipsAppSig(DirectTeXPro) $texAppSig(DirectTeXPro)
set dvipsAppScripts(DirectTeXPro) {
{dosc -c $quotedSig -s \
"ProjectMgr -t \$dt_TeXFormat \"[lindex [winNames -f] 0]\"; \
directory \$dt_TeXProjectDir > CurrDirectory; dvips \$dt_TeXProjectName"}
}
#--------------------------------------------------------------------------
# PS viewers
#--------------------------------------------------------------------------
# Mac GhostScript Viewer:
# set viewPSAppName(MacGS) {Mac GS*}
set viewPSAppSig(MacGS) {gsVR}
set viewPSAppScripts(MacGS) {
{dosc -c $quotedSig -k 'aevt' -e 'odoc' -r -f $filename}
}
# CMacTeX:
# set viewPSAppName(CMacTeX) viewps
set viewPSAppSig(CMacTeX) {CMT5}
set viewPSAppScripts(CMacTeX) {
{dosc -c $quotedSig -k 'aevt' -e 'odoc' -r -f $filename}
}
#--------------------------------------------------------------------------
# PS print drivers
#--------------------------------------------------------------------------
# CMacTeX:
# set printPSAppName(CMacTeX) printps
set printPSAppSig(CMacTeX) {PSP*}
set printPSAppScripts(CMacTeX) {
{dosc -c $quotedSig -k 'aevt' -e 'odoc' -r -f $filename}
}
# DirectTeX Pro:
# set printPSAppName(DirectTeXPro) $texAppName(DirectTeXPro)
set printPSAppSig(DirectTeXPro) $texAppSig(DirectTeXPro)
# set printPSAppScripts(DirectTeXPro) {
# {dosc -c $quotedSig -s "SelectApp; dvips \$dt_TeXProjectName; \
# download \$dt_TeXProjectName.ps"}
# }
set printPSAppScripts(DirectTeXPro) {
{dosc -c $quotedSig -s \
"ProjectMgr -t \$dt_TeXFormat \"[lindex [winNames -f] 0]\"; \
directory \$dt_TeXProjectDir > CurrDirectory; download \$dt_TeXProjectName.ps"}
}
# Drop•PS
# set printPSAppName(DropPS) {Drop•PS}
set printPSAppSig(DropPS) {D•PS}
set printPSAppScripts(DropPS) {
{dosc -c $quotedSig -k 'aevt' -e 'odoc' -r -f $filename}
}
#--------------------------------------------------------------------------
# BibTeX apps
#--------------------------------------------------------------------------
# CMacTeX:
# set bibtexAppName(CMacTeX) {*bibtex}
set bibtexAppSig(CMacTeX) {CMTu}
set bibtexAppScripts(CMacTeX) {
{sendOpenEvent noReply $quotedSig $filename}
}
# DirectTeX Pro:
# set bibtexAppSig(DirectTeXPro) $texAppSig(DirectTeXPro)
set bibtexAppScripts(DirectTeXPro) {
{dosc -c $quotedSig -s \
"ProjectMgr -t \$dt_TeXFormat \"[lindex [winNames -f] 0]\"; MenuCommand 1 5"}
}
# Vince Darley's BibTeX:
# set bibtexAppName(BibTeX) {BibTeX*}
set bibtexAppSig(BibTeX) {Vbib}
set bibtexAppScripts(BibTeX) {
{sendOpenEvent noReply $quotedSig $filename}
}
#--------------------------------------------------------------------------
# MakeIndex apps
#--------------------------------------------------------------------------
# CMacTeX:
# set makeindexAppName(CMacTeX) {[Mm]ake[Ii]ndex}
set makeindexAppSig(CMacTeX) {CMTt}
set makeindexAppScripts(CMacTeX) {
{sendOpenEvent noReply $quotedSig $filename}
}
# DirectTeX Pro:
# set makeindexAppName(DirectTeXPro) $texAppName(DirectTeXPro)
set makeindexAppSig(DirectTeXPro) $texAppSig(DirectTeXPro)
# set makeindexAppScripts(DirectTeXPro) {
# {dosc -c $quotedSig -s \
# "ProjectMgr -t LaTeX \"$filename\"; SelectApp; MenuCommand 1 6"}
# }
set makeindexAppScripts(DirectTeXPro) {
{dosc -c $quotedSig -s \
"ProjectMgr -t \$dt_TeXFormat \"[lindex [winNames -f] 0]\"; MenuCommand 1 6"}
}
# Rick Zaccone's MakeIndex:
# set makeindexAppName(MakeIndex) {MakeIndex*}
set makeindexAppSig(MakeIndex) {RZMI}
set makeindexAppScripts(MakeIndex) {
{sendOpenEvent noReply $quotedSig $filename}
}
#--------------------------------------------------------------------------
# Typeset submenu commands
#--------------------------------------------------------------------------
proc typeset {} {
# Is there a window open?
set currentWin [lindex [winNames -f] 0]
if { $currentWin == "" } {
typesetFile ""
return
}
# Is the current window part of TeX fileset?
set fset [isWindowInFileset $currentWin "tex"]
if { $fset != "" } {
if [dirtyFileset $fset] {
switch [askyesno -c "Save current TeX fileset?"] {
"yes" {saveEntireFileset $fset}
"no" {
# do nothing
}
"cancel" {return}
}
}
typesetFile [texFilesetBaseName $fset]
return
}
# Is the window untitled or dirty?
global PREFS
set currentDoc [file tail $currentWin]
if { [set num [winUntitled]] } {
switch [askyesno -c "Save \"$currentDoc\"?"] {
"yes" {
if {[catch {set currentWin [saveAs "Untitled$num\.tex"]}]} then {return}
}
"no" {
set tmpFilename "Untitled$num\.tex"
set text [getText 0 [maxPos]]
if { [file exists "$PREFS:tmp"] == 0 } { mkdir "$PREFS:tmp" }
set newDoc "$PREFS:tmp:$tmpFilename"
writeFile $newDoc $text 1
set currentWin $newDoc
}
"cancel" {return}
}
} elseif { [winDirty] } {
switch [askyesno -c "Save \"$currentDoc\"?"] {
"yes" {save}
"no" {
set text [getText 0 [maxPos]]
if { [file exists "$PREFS:tmp"] == 0 } { mkdir "$PREFS:tmp" }
set newDoc "$PREFS:tmp:temp-$currentDoc"
writeFile $newDoc $text 1
set currentWin $newDoc
}
"cancel" {return}
}
}
# Is the current window TeX-able?
set ext [file extension $currentWin]
if { [lsearch -exact {.tex .ltx .dtx .ins} $ext] < 0 } {
typesetFile ""
return
}
# Strip off trailing garbage (if any) and typeset:
regexp {(.*) <[0-9]+>} $currentWin dummy currentWin
typesetFile $currentWin
}
proc typesetSelection {} {
global PREFS
if { [isSelection] } then {
watchCursor
message "processing selection…"
set currentWin [lindex [winNames -f] 0]
# Is the current window part of TeX fileset?
set fset [isWindowInFileset $currentWin "tex"]
if { $fset == "" } {
set pos1 [getPos]
if { [isInDocument] } then {
set pos2 [selEnd]
if { [isInDocument] } then {
# fall through
} else {
beep
set msg "Selection not in document environment. Continue?"
if { [askyesno $msg] == "no" } then { return }
}
} else {
beep
set msg "Selection not in document environment. Continue?"
if { [askyesno $msg] == "no" } then { return }
set pos2 [selEnd]
}
set body "\r[getText $pos1 $pos2]\r"
set searchText [getText 0 [maxPos]]
} else {
set body "\r[getSelect]\r"
# Will not handle a base file that is open and dirty:
set searchText [buildFilecontents [texFilesetBaseName $fset]]
}
message "building temporary document…"
set pattern {(\\documentclass.*)\\begin\{document\}}
if { ![regexp $pattern $searchText dummy preamble] } then {
set preamble "\\documentclass\{article\}\r"
}
set rootFile [file rootname $currentWin]
set tempFile "temp-[file tail $rootFile]"
set auxFile $rootFile.aux
if { [file exists $auxFile] } {
set latexDoc [buildFilecontents $auxFile $tempFile.aux]
} else {
set latexDoc {}
}
append latexDoc $preamble [buildEnvironment "document" "" $body "\r"]
set currentDir [file dirname $currentWin]
set latexDoc [texResolveAll $latexDoc $currentDir]
if { [file exists "$PREFS:tmp"] == 0 } { mkdir "$PREFS:tmp" }
set newFile "$PREFS:tmp:$tempFile.tex"
writeFile $newFile $latexDoc 1
typesetFile $newFile
} else {
beep
message "no selection"
}
}
proc typesetClipboard {} {
global PREFS
set body "\r[getScrap]\r"
set pat1 {\\begin\{document\}.*\\end\{document\}}
set pat2 {\\documentclass}
set preamble "\\documentclass\{article\}\r"
# Check to see if there's a document environment:
if {![regexp $pat1 $body]} then {
append text $preamble [buildEnvironment "document" "" $body "\r"]
} else {
# Check to see if there's a \documentclass command:
if {![regexp $pat2 $body]} then {
append text $preamble $body
} else {
set text $body
}
}
if { [file exists "$PREFS:tmp"] == 0 } { mkdir "$PREFS:tmp" }
set newFile "$PREFS:tmp:temp-noname\.tex"
writeFile $newFile $text 1
set currentWin $newFile
typesetFile $currentWin
}
# Typeset $filename, but perform no error-checking
#
proc typesetFile {filename} {
if { $filename == "" } {
set filename [getfile "Choose a file to typeset:"]
}
global TeXmodeVars
set prompt {TeX app}
set flag $TeXmodeVars(runTeXInBack)
evalTeXScript tex $prompt $filename $flag
}
# Apply $op to a file with extension $ext. (See latexMenu.tcl for
# many examples.) If 'forcecurrent == 1', use the current window
# even if it belongs to a TeX fileset.
#
proc doTypesetCommand {op ext {forcecurrent 0}} {
if { [set filename [findAuxiliaryFile $ext $forcecurrent]] != "" } {
if { $op == "open" } {
edit -r -m -w $filename
} else {
$op${ext}File $filename
}
} else {
beep
alertnote "No $ext file found!"
}
}
proc viewDVIFile {filename} {
set prompt {DVI viewer}
evalTeXScript viewDVI $prompt $filename
}
proc printDVIFile {filename} {
set prompt {DVI print driver}
evalTeXScript printDVI $prompt $filename
}
proc dvipsDVIFile {filename} {
set prompt {DVI-to-PS filter}
evalTeXScript dvips $prompt $filename
}
proc viewPSFile {filename} {
set prompt {PS viewer}
evalTeXScript viewPS $prompt $filename
}
proc printPSFile {filename} {
set prompt {PS print driver}
evalTeXScript printPS $prompt $filename
}
proc bibtexAUXFile {filename} {
set prompt {BibTeX app}
evalTeXScript bibtex $prompt $filename
}
proc makeindexIDXFile {filename} {
set prompt {MakeIndex app}
evalTeXScript makeindex $prompt $filename
}
proc evalTeXScript {op prompt filename {runAppInBackground 0}} {
global ${op}Sig ${op}AppSig ${op}AppScripts
set supportedApps [array names ${op}AppSig]
foreach app $supportedApps { lappend sigs [set ${op}AppSig($app)] }
set longPrompt "Please locate a $prompt."
if { [catch {launchBackApplSigs $sigs ${op}Sig $longPrompt} appname] } {
error "bug in 'launchBackApplSigs'"
}
set sig [set ${op}Sig]
set quotedSig "'[string trim $sig {'}]'"
# if { $runAppInBackground == 0 } { switchTo [file tail $appname] }
if { $runAppInBackground == 0 } { switchTo $quotedSig }
foreach app $supportedApps {
if { [set ${op}AppSig($app)] == $sig } {
foreach script [set ${op}AppScripts($app)] {
eval $script
}
return
}
}
beep
alertnote "Sorry, no support for your $prompt."
return
}
# Two bugs in 'getfile' (see "alpha.bugs58"):
proc openAnyTeXFile {} {
set currentWin [lindex [winNames -f] 0]
cd [file dirname $currentWin]
set filename [getfile ""]
if { ![string length $filename] } return
edit -r -m -w $filename
}
proc removeAuxiliaryFiles {} {
set word ""
set removeSilently 0
set currentWin [lindex [winNames -f] 0]
if { $currentWin == "" } { return }
set currentDir [file dirname $currentWin]
set extensions {.ps .dvi .log .aux .bbl .idx .ind .glo .gls \
.toc .lof .lot .blg .ilg}
foreach ext $extensions {
message "Checking for *$ext files…"
set files [glob -nocomplain "$currentDir:*$ext"]
foreach file $files {
set word " more"
if {$removeSilently} {
if {[catch {rm "$currentDir:*$ext"}]} then {
alertnote "not all \"*$ext\" files deleted"
}
break
} else {
message ""
set filename [file tail $file]
switch [buttonAlert "Remove \"$filename\"?" "yes" "no" {rm ext} {rm all} "cancel"] {
"yes" {
message "Removing $filename…"
if {[catch {removeFile "$currentDir:$filename"}]} then {
alertnote "\"$filename\" not deleted"
} else {
message $filename
}
}
"no" {}
"{rm ext}" {
if {[catch {rm "$currentDir:*$ext"}]} then {
alertnote "not all \"*$ext\" files deleted"
}
break
}
"{rm all}" {
if {[catch {rm "$currentDir:*$ext"}]} then {
alertnote "not all \"*$ext\" files deleted"
}
set removeSilently 1
break
}
"cancel" {return}
}
}
}
}
message "No$word files found"
}
#--------------------------------------------------------------------------
# Utility procs:
#--------------------------------------------------------------------------
# Find a LaTeX auxiliary file with extension $ext. If 'forcecurrent'
# is true, search the current directory without checking for TeX filesets.
#
proc findAuxiliaryFile {ext {forcecurrent 0}} {
set currentWin [lindex [winNames -f] 0]
if { $currentWin == "" } { return "" }
set currentDoc [file tail $currentWin]
if $forcecurrent {
# pretend there are no TeX filesets:
set fset ""
} else {
set fset [isWindowInFileset $currentWin "tex"]
}
if { $fset != "" } {
set currentWin [texFilesetBaseName $fset]
set currentDoc [file tail $currentWin]
set currentDir [file dirname $currentWin]
set docBasename [file rootname $currentDoc]
set lowerExt [string tolower $ext]
} else {
# we do all this if it's not a project:
set currentDir [file dirname $currentWin]
set docBasename [file rootname $currentDoc]
set lowerExt [string tolower $ext]
# Is the window untitled or dirty?
global PREFS
if { [set num [winUntitled]] } {
set filename $PREFS:tmp:Untitled$num\.$lowerExt
if { [file exists $filename] } {
return $filename
} else {
return ""
}
} elseif { [winDirty] } {
switch [askyesno "Window dirty---continue anyway?"] {
"yes" {
set filename $PREFS:tmp:temp-$currentDoc\.$lowerExt
if { [file exists $filename] } {
return $filename
} else {
# fall through
}
}
"no" {return ""}
}
}
}
# Check the current directory:
set filename $currentDir:$docBasename\.$lowerExt
if { [file exists $filename] } {
return $filename
} else {
return ""
}
}
# If the current window is untitled, return its number (i.e., either
# the number 1 or the number n in "Untitled <n>"); otherwise, return 0.
proc winUntitled {} {
set currentWin [lindex [winNames -f] 0]
if { $currentWin == "" } { return 0 }
set currentDoc [file tail $currentWin]
if { [string match $currentWin $currentDoc] } {
if { [regexp {<(.*)>} $currentDoc dummy num] } {
return $num
} else {
return 1
}
} else {
return 0
}
}